const mongoose = require('mongoose');

/**
 * 基本数据配置
 * @type {{port: *, pass: *, name: *, dataName: *, url: *}}
 */
const mongooData = {
  url: process.env.MONGODB_HOST,
  port: process.env.MONGODB_PORT,
  name: process.env.MONGODB_ACCOUNT,
  pass: process.env.MONGODB_PASS,
  dataName: process.env.MONGODB_DB_NAME
}

/**
 * 数据库链接URL
 * @type {string}
 */
const url = `mongodb://${mongooData.name}:${mongooData.pass}@${mongooData.url}:${mongooData.port}/${mongooData.dataName}`

console.log('MongoDB => ', url)

/**
 * 连接数据库
 */
mongoose.connect(url, {useNewUrlParser:true});

/**
 * 链接的数据库对象
 */
const db = mongoose.connection

/**
 * 链接成功回调
 */
db.on("connected", function () {
  console.log("MongoDB 链接成功")
});

/**
 * 链接失败回调
 */
db.on("error", function (e) {
  console.log("MongoDB 连接失败")
  console.log(e)
});

/**
 * 断开连接回调
 */
db.on("disconnected", function (e) {
  console.log("MongoDB 断开连接")
	console.log(e)
});

